package com.fyy.quartz.controller;

import com.fyy.quartz.model.dto.QuartzDto;
import com.fyy.quartz.service.QuartzJobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author fyy
 * @Description
 * @Date 2020/12/23 11:32
 */
@RestController
@RequestMapping("/quartz")
public class QuartzController {

    private static Logger logger = LoggerFactory.getLogger(QuartzController.class);
    @Autowired
    private QuartzJobService quartzService;

    //添加新任务
    @RequestMapping("/addJob")
    public Object addJob(@RequestBody QuartzDto quartzDto){
        quartzService.addJob(quartzDto.getJobClass(),quartzDto.getJobName()
                ,quartzDto.getGroupName(),quartzDto.getCronExpression(),quartzDto.getParam());
        return HttpStatus.OK;
    }

    //暂停任务
    @RequestMapping("/pauseJob")
    public Object pauseJob(@RequestBody QuartzDto quartzDto){
        quartzService.pauseJob(quartzDto.getJobName(),quartzDto.getGroupName());
        return HttpStatus.OK;
    }

    //恢复任务
    @RequestMapping("/resumeJob")
    public Object resumeJob(@RequestBody QuartzDto quartzDto){
        quartzService.resumeJob(quartzDto.getJobName(),quartzDto.getGroupName());
        return HttpStatus.OK;
    }

    //立即运行一次定时任务
    @RequestMapping("/runOnceJob")
    public Object runOnceJob(@RequestBody QuartzDto quartzDto){
        quartzService.runOnce(quartzDto.getJobName(),quartzDto.getGroupName());
        return HttpStatus.OK;
    }

    //更新任务
    @RequestMapping("/updateJob")
    public Object updateJob(@RequestBody QuartzDto quartzDto){
        quartzService.updateJob(quartzDto.getJobName(),quartzDto.getGroupName()
                ,quartzDto.getCronExpression(),quartzDto.getParam());
        return HttpStatus.OK;
    }

    //删除任务
    @RequestMapping("/delJob")
    public Object delJob(@RequestBody QuartzDto quartzDto){
        quartzService.deleteJob(quartzDto.getJobName(),quartzDto.getGroupName());
        return HttpStatus.OK;
    }

    //启动所有任务
    @RequestMapping("/startAllJob")
    public Object startAllJob(){
        quartzService.startAllJobs();
        return HttpStatus.OK;
    }

    //暂停所有任务
    @RequestMapping("/pauseAllJob")
    public Object pauseAllJob(){
        quartzService.pauseAllJobs();
        return HttpStatus.OK;
    }

    //恢复所有任务
    @RequestMapping("/resumeAllJob")
    public Object resumeAllJob(){
        quartzService.resumeAllJobs();
        return HttpStatus.OK;
    }

    //关闭所有任务
    @RequestMapping("/shutdownAllJob")
    public Object shutdownAllJob(){
        quartzService.shutdownAllJobs();
        return HttpStatus.OK;
    }

}
